<?php
class ScenicSpot extends Data_Item
{
    public function __construct ($id = NULL)
    {
        $this->_commTableName  = 'mod_scenic_spot';
        $this->_commPrimaryKey = 'id';

        parent::__construct();

        $this->_scenicId = (int) $id;
        $this->_nid      = $id;
    }
    
    public function detail ($id = NULL)
    {
        $id = $id > 0 ? $id : $this->_scenicId ;
        $detail = parent::detail($id);

        if (empty($detail))
            return NULL;

        $detail['typeName'] = App_Config::getLabelString($detail['type'], 'array');
        return $detail;
    }

    public function setGeoLocation ($lat, $lng)
    {
        $rows = DB::update('mod_scenic_spot')
            ->set(array(
                'lat' => $lat, 'lng' => $lng))
            ->where('id', '=', $this->_scenicId)
            ->limit(1)
            ->execute();

        return $rows > 0;

    }

    // 返回路线所在城市中，没有被在路线中使用的，和属于用户自定义的景点列表
    //用户自定义景点，managerUid=0,guiderUid=自身uid
    //系统景点（来自于用户自定义景点），managerUid=4,guiderUid=自身uid
    //系统景点（来自于系统），managerUid=4,guiderUid=0
    public function validListByTrack ($trackId, $guiderUid,$cityId)
    {
        $track = new App_Track($trackId);
        $scenicIds = $track->getScenicIds();
        if (empty($scenicIds))
            $scenicIds = array(-1);

        $query = DB::select('s.id', 's.thumb', 's.type', 's.name', 's.subTitle', 's.iconUrl',array('t.name','typeName'))
            ->from(array('mod_scenic_spot','s'))
            ->join(array('mod_scenic_type','t'),'left')
            ->on('s.type','=','t.id')
            ->where('s.status', '=', 'VALID')
            ->where('s.id', 'not in', $scenicIds)
            ->where('s.cityId','=',$cityId)
            ->and_where_open()
            ->where('s.managerUid', '>', 0)
            ->or_where('s.guiderUid', '=', $guiderUid)
            ->and_where_close()
            ->order_by('s.type', 'asc')
            ->execute();
       
        
        $ret   = array();
        foreach ($query as $row){
        	$ret[$row['type']]['typeId']=$row['type'];
        	$ret[$row['type']]['typeName']=$row['typeName'];
        	$ret[$row['type']]['scenics'][]=$row;
        }
        //如果用户没有自定义景点，创建自定义的空目录
        if(!isset($ret[2])){
        	$ret[2]['typeId']=2;
        	$ret[2]['typeName']="您的自定义景点";
        	$ret[2]['scenics']=array();
        }
        return array_values($ret);
    }

    // 搜索
    public function search ( $cityId = NULL, $intType = NULL, $offset = 0, $limit = 10)
    {
        $query = DB::select('id', 'thumb', 'summary', 'name', 'subTitle', 'created', 'iconUrl')
            ->from('mod_scenic_spot')
            ->where('status', '=', 'VALID')
            ->offset($offset)
            ->limit($limit)
            ->order_by('created', 'desc');

        $count = DB::select(array('count("id")', 'counts'))
            ->from('mod_scenic_spot')
            ->where('status', '=', 'VALID');

        if (! empty($cityId))
        {
           $query= $query->where('cityId', '=', (int)$cityId);
           $count= $count->where('cityId', '=', (int)$cityId);
        }
        //排除自定义景点
        if (empty($intType)||$intType<0)
        {
           $query= $query->where('type', '!=', 2);
           $count= $count->where('type', '!=', 2);
        }

        if ($intType > 0)
        {
            $query=$query->where('type', '=', (int) $intType);
            $count=$count->where('type', '=', (int) $intType);
        }

        $query = $query->execute();
        $counts= $count->execute()->get('counts');

        $ret   = array();
        foreach ($query as $rs)
        {
            $ret[ $rs['id'] ] = $rs;
        }

        return array((int) $counts, $ret);
    }

    public function remove ()
    {
        $this->update(array('status' => 'INVALID'), array('id', '=', $this->_scenicId));
    }

    public function setIconUrl ($url)
    {
        $this->update(array('iconUrl' => $url), array('id', '=', $this->_scenicId));
    }

    public function addByGuider ($guiderUid, $newdata)
    {
        $newdata['iconUrl'] = '/resource/www/imgs/icon/0.png'; 
        $newdata['created'] = DB::expr('unix_timestamp(now())');
        $newdata['changed'] = DB::expr('unix_timestamp(now())');
        $newdata['managerUid'] = 0;
        $newdata['status']    = 'VALID';
        $newdata['guiderUid'] = $guiderUid;

        list($insert_id, $rows) = DB::insert('mod_scenic_spot')
            ->columns (array_keys($newdata))
            ->values (array_values($newdata))
            ->execute();

        return $insert_id;
    }
}
